// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal class NoGenericModelAccessor : NoGenericModelAccessor<object>
    {
        public NoGenericModelAccessor(IModelAccessor<object> modelAccessor) : base(modelAccessor)
        {
        }

        public NoGenericModelAccessor(IModelFactory<object> factory, Type modelType) : base(factory, modelType)
        {
        }
    }

    internal class NoGenericModelAccessor<TModel> : IModelAccessor
    {
        private readonly IModelAccessor<TModel> _accessor;

        public NoGenericModelAccessor(IModelFactory<TModel> factory, Type modelType)
            : this(new PocoModelAccessor<TModel>(factory, modelType))
        {
        }

        public NoGenericModelAccessor(IModelAccessor<TModel> modelAccessor)
        {
            _accessor = modelAccessor;
        }

        public IModelMeta ModelMeta => _accessor.ModelMeta;

        public int IndexOf(string name)
        {
            return _accessor.IndexOf(name);
        }

        public object GetValue(object model, int index)
        {
            return _accessor.GetValue((TModel)model, index);
        }

        public void SetValue(object model, int index, object value)
        {
            _accessor.SetValue((TModel)model, index, value);
        }

        public object GetValue(object model, string name)
        {
            return _accessor.GetValue((TModel)model, name);
        }

        public void SetValue(object model, string name, object value)
        {
            _accessor.SetValue((TModel)model, name, value);
        }

        public object ToModel(IModelEntry<object> modelEntry, bool current = true)
        {
            return _accessor.ToModel((IModelEntry<TModel>)modelEntry, current);
        }

        public object Clone(object model)
        {
            return _accessor.Clone((TModel)model);
        }

        public void CopyModel(object toModel, object fromModel)
        {
            _accessor.CopyModel((TModel)toModel, (TModel)fromModel);
        }

        public bool ValueEquals(object model1, object model2)
        {
            return _accessor.ValueEquals((TModel)model1, (TModel)model2);
        }
    }
}
